/*
 * driver: uart
 * File: $Id: uarttask.c
 *
 *  Created on: 2019年1月4日
 *      Author: hongwei
 */

#include "uarttask.h"
#include "main.h"
#include "FreeRTOS.h"
#include "cmsis_os.h"


void data_handlingTask(void const *argument);
osMessageQId UARTQueueHandle;
osThreadId datahandlingTaskHandle;
static T_UARTRMSG uart1rmsg, uart2rmsg, uart3rmsg;
void uartconfig(void)
{
	T_UARTRMSG *uartrx;

	osMessageQDef(uartQueue, 10, uint8_t);
	UARTQueueHandle = osMessageCreate(osMessageQ(uartQueue), NULL);

	osThreadDef(datahandlingTask, data_handlingTask, osPriorityRealtime, 0, 512);
	datahandlingTaskHandle = osThreadCreate(osThread(datahandlingTask), NULL);

	uartrx = &uart1rmsg;
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
	HAL_UART_Receive_DMA(&huart1, uartrx->rxbuff, uartbufflength);

//	uartrx = &uart2rmsg;
//	__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
//	HAL_UART_Receive_DMA(&huart2, uartrx->rxbuff, uartbufflength);

//	uartrx = &uart3rmsg;
//	__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
//	HAL_UART_Receive_DMA(&huart3, uartrx->rxbuff, uartbufflength);

}

void data_handlingTask(void const *argument)
{
	T_UART_CHANNEL huartch;
	T_UARTRMSG *uartrx;
	osEvent evt;
	for (;;)
	{

		evt = osMessageGet(UARTQueueHandle, osWaitForever); // wait for message Forever
		if (evt.status == osEventMessage)
		{
			huartch = (T_UART_CHANNEL)evt.value.v;
			switch (huartch)
			{
			case CHANNEL1:
				uartrx = &uart1rmsg;
				uart1_rxcallback(uartrx->rxbuff, uartrx->length);
				__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
				break;
//			case CHANNEL2:
//				uartrx = &uart2rmsg;
//				uart2_rxcallback(uartrx->rxbuff, uartrx->length);
//				__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
//				break;
//			case CHANNEL3:
//				uartrx = &uart3rmsg;
//				uart3_rxcallback(uartrx->rxbuff, uartrx->length);
//				__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
//				break;
			default:
				break;
			}
		}
		osDelay(1);
	}
}

void USART_RxIdleCallback(UART_HandleTypeDef *huart)
{
	T_UARTRMSG *uartrx;
	T_UART_CHANNEL huartch;
	if (huart == &huart1)
	{
		if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
		{
			huartch = CHANNEL1;
			uartrx = &uart1rmsg;
			HAL_UART_AbortReceive(huart);
			__HAL_UART_CLEAR_IDLEFLAG(huart);
			uartrx->length = uartbufflength - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
			HAL_UART_Receive_DMA(huart, uartrx->rxbuff, uartbufflength);
			__HAL_UART_DISABLE_IT(huart, UART_IT_IDLE);
			osMessagePut(UARTQueueHandle, huartch, 0U);
		}
	}
//	if (huart == &huart2)
//	{
//		if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
//		{
//			huartch = CHANNEL2;
//			uartrx = &uart2rmsg;
//			HAL_UART_AbortReceive(huart);
//			__HAL_UART_CLEAR_IDLEFLAG(huart);
//			uartrx->length = uartbufflength - __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);
//			HAL_UART_Receive_DMA(huart, uartrx->rxbuff, uartbufflength);
//			__HAL_UART_DISABLE_IT(huart, UART_IT_IDLE);
//			osMessagePut(UARTQueueHandle, huartch, 0U);
//		}
//	}
//
//	if (huart == &huart3)
//	{
//		if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
//		{
//			huartch = CHANNEL3;
//			uartrx = &uart3rmsg;
//			HAL_UART_AbortReceive(huart);
//			__HAL_UART_CLEAR_IDLEFLAG(huart);
//			uartrx->length = uartbufflength - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);
//			HAL_UART_Receive_DMA(huart, uartrx->rxbuff, uartbufflength);
//			__HAL_UART_DISABLE_IT(huart, UART_IT_IDLE);
//			osMessagePut(UARTQueueHandle, huartch, 0U);
//		}
//	}

}
